package com.xilihui.xlh.business.adapters;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.BookingDetailsActivity;
import com.xilihui.xlh.business.activitys.EvaluationStoreActivity;
import com.xilihui.xlh.business.activitys.FeedbackRecordActivity;
import com.xilihui.xlh.business.entities.HistoryEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;

import org.feezu.liuli.timeselector.Utils.TextUtil;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/6/29.
 */

public class HistoryAdapter extends RecyclerView.Adapter<BaseViewHolder>{
    
    ArrayList<HistoryEntity.AppointBean> datas = new ArrayList<>();
    Activity activity;
    
    public HistoryAdapter(Activity activity){
        this.activity = activity;
    }
    
    int status = 0;
    
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_me_appointment,
                parent,false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        holder.setText(R.id.tv_shopName,datas.get(position).getAgent().getName());
        final String status = datas.get(position).getStatus();
        if(status.equals("3")){
            holder.setText(R.id.tv_waitStart,"已完成");
            holder.setText(R.id.tv_startAppointment,"评价门店");
        }else if(status.equals("4")){
            holder.setText(R.id.tv_waitStart,"已取消");
            holder.setVisibility(R.id.tv_cancle,View.GONE);
            holder.setVisibility(R.id.tv_take,View.GONE);
            holder.setText(R.id.tv_startAppointment,"删除");
        }
        holder.setText(R.id.iv_address,datas.get(position).getAgent().getAddress());
        
        holder.setOnClickListener(R.id.iv_phone, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + datas.get(position).getAgent().getPhone());
                intent.setData(data);
                view.getContext().startActivity(intent);
            }
        });
        
        holder.setText(R.id.tv_time,datas.get(position).getStart_time()+
        datas.get(position).getEnd_time());
        holder.setText(R.id.tv_type,datas.get(position).getHair_project().getName());
        holder.setText(R.id.tv_seat,datas.get(position).getSeat().getName());
        holder.setText(R.id.tv_beizhu,datas.get(position).getRemark());

        holder.setVisibility(R.id.tv_cancle,View.GONE);
        holder.setText(R.id.tv_take,"查看详情");
        holder.setOnClickListener(R.id.tv_take, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(),BookingDetailsActivity.class);
                intent.putExtra("data",datas.get(position));
                view.getContext().startActivity(intent);
            }
        });
        
        if(datas.get(position).getComment_store().equals("1")){
            holder.setVisibility(R.id.tv_startAppointment,View.GONE);
        }else {
            holder.setVisibility(R.id.tv_startAppointment,View.VISIBLE);
        }
        
        String remark = datas.get(position).getRemark();
        if(TextUtil.isEmpty(remark)){
            holder.setVisibility(R.id.tv_beizhu,View.GONE);
        }else {
            holder.setText(R.id.tv_beizhu,remark);
        }
       
        holder.setOnClickListener(R.id.tv_startAppointment, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(status.equals("3")){
                    Intent intent = new Intent(view.getContext(),EvaluationStoreActivity.class);
                    intent.putExtra("data",datas.get(position));
                    view.getContext().startActivity(intent);
                }else if(status.equals("4")){
                    delete(position);
                }
            }
        });
        
    }

    @Override
    public int getItemCount() {
        if(datas!=null&&datas.size()>0){
            return datas.size();
        }else {
            return 0;
        }
    }
    
    public void setDatas(ArrayList<HistoryEntity.AppointBean> data){
        if (datas!=null&&datas.size()>0){
            datas.clear();
        }
        datas.addAll(data);
        notifyDataSetChanged();
    }
    
    public void delete(final int position){
        MyAppointmentRequest.deleteDistory(activity,datas.get(position).getId())
                .compose(DoTransform.<BaseEntity>applyScheduler((FragmentActivity) activity,true))
                .subscribe(new HttpSubscriber<BaseEntity>(activity) {
                    @Override
                    protected void onSuccess(BaseEntity entity) {
                        ToastUtil.toastShortPositive(entity.getMsg());
                        datas.remove(position);
                        notifyDataSetChanged();
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                    }
                });
    }
    
    
}
